import 'package:flutter/material.dart';
import 'package:flutter_massage/common_lib/index.dart';
import 'package:flutter_massage/pages/main/main_logic.dart';
import 'package:flutter_massage/values/colors.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class MainPage extends StatefulWidget {
  const MainPage({Key? key}) : super(key: key);

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  late final MainLogic mainLogic;
  final double tabHeight = 50;

  @override
  void initState() {
    super.initState();
    mainLogic = Get.put(MainLogic());
  }

  @override
  void dispose() {
    super.dispose();
    Get.delete<MainLogic>();
  }

  @override
  Widget build(BuildContext context) {
    return BaseScaffold(
      resizeToAvoidBottomInset: false,
      child: GetBuilder<MainLogic>(
        builder: (logic) {
          return Column(
            children: [
              Expanded(
                child: PageView(
                  physics: const NeverScrollableScrollPhysics(),
                  controller: mainLogic.pageController,
                  children: mainLogic.pageList,
                ),
              ),
              if (logic.showTabBar)
                Container(
                  padding: EdgeInsets.only(bottom: ScreenUtil().bottomBarHeight * 0.5),
                  height: tabHeight + ScreenUtil().bottomBarHeight,
                  child: Container(
                    decoration: CommonDecoration.getBorder(top: BorderSide(width: 0.5.w, color: '#D7D7D7'.toColor)),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: mainLogic.items.map((e) => Expanded(child: _item(e))).toList(),
                    ),
                  ),
                ),
            ],
          );
        },
      ),
    );
  }

  Widget _item(BottomBarItem item) {
    bool selected = mainLogic.currentIndex == mainLogic.items.indexOf(item);
    Color color = selected ? AppColors.primaryGreen : AppColors.textLevelOne;
    String icon = selected ? item.selectedIcon : item.icon;
    return ButtonWidget(
      height: tabHeight,
      onTap: () => mainLogic.onJumpToPage(item.name),
      hasInkWell: false,
      child: Column(
        children: [
          IconWidget.svg(icon, width: 22, color: color, badgeString: item.badgeNum),
          const SizedBox(height: 2),
          TextWidget.oneLine(text: item.name, color: color, fontSize: 12),
        ],
      ),
    );
  }
}
